﻿define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Myfriend, $ionicSlideBoxDelegate,GoDialog,$ionicPopup,$timeout,GoPage,Auth) {
		
		
		$scope.$on('$ionicView.afterEnter', function() {
			$scope.init_data_auth();
			$scope.init_data();
		}, false);
		
		$scope.scope = new Object();//数据模型
		//滑动切换
		$scope.selectTab = 0;
		$scope.activeSlide = function(index) {
			$scope.selectTab = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
		};
		//滑动切换
		$scope.slideChanged = function(index) {
			$scope.selectTab = index;
		};
		
		//数据初始化
		$scope.init_data = function() {
			//已同意
			Myfriend.get_friend_list(1,1,function(data) {
				$scope.friends_agree = data;
			});
			//已拒绝
			Myfriend.get_friend_list(2,1,function(data) {
				$scope.friends_disagree = data;
			});
			//待确认
			Myfriend.get_apply_list(1,function(data) {
				$scope.friends_apply = data;
			})
			//黑名单
			Myfriend.get_black_list(1,function(data){
				$scope.friends_black = data;
			})
		}
			
		
		//页面跳转
		$scope.onshow_first = function(store_id){
			if(store_id){
				GoPage.go_states("first",{"index":0,"store_id":store_id});
			}
		}
		
		//同意--做好
		$scope.agree = function(id) {
			Myfriend.get_agree(id, function(data) {
				if(data.success) {
					GoDialog.toast("添加成功！");
					$scope.init_data();
				}else{
					GoDialog.toast(data.msg);
				}
			})
		}
		
		//删除好友--做好(黑名单不需要删除)
		$scope.delete = function(id){
			Myfriend.get_delete(id,function(data){
				if(data.success) {
					GoDialog.toast("删除成功！");
					$scope.init_data();
				} else{
					GoDialog.toast(data.msg);
				}
			})
		}
		
		//取消拉黑--做好
		$scope.cancleDefriend = function(member_id){
			console.log(member_id)
			Myfriend.cancleDefriend(member_id,function(data){
				if(data.success){
					GoDialog.toast("取消黑名单成功！");
					$scope.init_data();
					console.log(data)
				}else{
					GoDialog.toast(data.msg);
				}
			})
		}
		
		//拒绝
		$scope.refuse = function(id,reason){
			Myfriend.refuse(id,reason,function(data){
				if(data.success){
					GoDialog.toast("拒绝成功！");
					$scope.init_data();
					$scope.scope.reason = "";
				}else{
					GoDialog.toast(data.msg);
				}
			})
		}
		
		//拉黑--做好
		$scope.defriend = function(id,reason){
			Myfriend.defriend(id,reason,function(data){
				if(data.success){
					GoDialog.toast("拉黑成功！");
					$scope.init_data();
					$scope.scope.reason = "";
				}else{
					GoDialog.toast(data.msg);
				}
			})
		}
		
		//拉黑--做好
		$scope.scope.reason = "";
		var myPopup_defriend;
		$scope.showPopup_defriend = function(member_id) {
			myPopup_defriend = $ionicPopup.show({ //show()
				templateUrl: 'templates/modal/resean.html',
				title: "拉黑理由",
				scope: $scope, //绑定数据，必须写这行
				buttons: [{
					text: "取消",
					type: "button-positive", //定义按钮的样式
					onTap: function() { // 当点击取消按钮时，执行该函数

					}
				}, {
					text: "保存",
					type: "button-assertive",
					onTap: function() { // 当点击保存按钮时，执行该函数
						$scope.defriend(member_id,$scope.scope.reason)
					}
				}]
			});
		};
		
		//拒绝
		var myPopup_refuse;
		$scope.showPopup_refuse = function(member_id) {
			myPopup_refuse = $ionicPopup.show({ //show()
				templateUrl: 'templates/modal/resean.html',
				title: "拒绝理由",
				scope: $scope, //绑定数据，必须写这行
				buttons: [{
					text: "取消",
					type: "button-positive", //定义按钮的样式
					onTap: function() { // 当点击取消按钮时，执行该函数

					}
				}, {
					text: "保存",
					type: "button-assertive",
					onTap: function() { // 当点击保存按钮时，执行该函数
						$scope.refuse(member_id,$scope.scope.reason)
					}
				}]
			});
		};
		
		//显示拉黑理由
		var myPopup_reason;
		$scope.showPopup_reason = function(reason) {
			$scope.showDefriendReason = reason;
			myPopup_refuse = $ionicPopup.show({
				templateUrl: 'templates/modal/showDefriendReason.html',
				title: "拉黑理由",
				scope: $scope, //绑定数据，必须写这行
				buttons: [{
					text: "确定",
					type: "button-assertive",
					onTap: function() { // 当点击保存按钮时，执行该函数
					}
				}]
			});
		};
		
		var htmlEl = angular.element(document.querySelector('html'));
		htmlEl.on('click', function(event) {　　
			if(event.target.nodeName === 'HTML') {　　　　
				if(myPopup_defriend) {　　　　　　
					myPopup_defriend.close();　　　　
				}
				if(myPopup_refuse) {　　　　　　
					myPopup_refuse.close();　　　　
				}　　　　　　　　　
			}
		});
		
		
		/*---------------上下拉刷新------------------------*/
		$scope.nPage_agree = 1;
		$scope.nPage_disagree = 1;
		$scope.nPage_apply = 1;
		$scope.nPage_black = 1;
		$scope.isLoad = false;
		$scope.doRefresh = function(){
			$scope.isLoad = true;
			if($scope.selectTab==0){//===0
				Myfriend.get_friend_list(1,1,function(data) {
					if(data!=null){
						$scope.friends_agree = data;
					}else{
						$scope.friends_agree = new Array();
					}
					$scope.nPage_agree = 1;
				},function() {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
			}//==0
			
			if($scope.selectTab==1){//===1
				Myfriend.get_friend_list(2,1,function(data) {
					if(data!=null){
						$scope.friends_disagree = data;
					}else{
						$scope.friends_disagree = new Array();
					}
					$scope.nPage_disagree = 1;
				},function() {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
			}//==1
			if($scope.selectTab==2){//===2
				Myfriend.get_apply_list(1,function(data) {
					if(data!=null){
						$scope.friends_apply = data;
					}else{
						$scope.friends_apply = new Array();
					}
					$scope.nPage_apply = 1;
				},function(){
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
			}//==2
			if($scope.selectTab==3){//===3
				Myfriend.get_black_list(1,function(data) {
					if(data!=null){
						$scope.friends_black = data;
					}else{
						$scope.friends_black = new Array();
					}
					$scope.nPage_black = 1;
				},function() {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
			}//==3
			
		}
		
		$scope.doRefreshMore = function(){
			$scope.isLoad = true;
			if($scope.selectTab==0){//==0
				if($scope.friends_agree.page.count<=$scope.friends_agree.member.length){
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
					return;
				}
				Myfriend.get_friend_list(1,$scope.nPage_agree+1,function(data) {
					if(data!=null){
						if($scope.friends_agree.member!=undefined){
							$scope.friends_agree.member = $scope.friends_agree.member.concat(data.member);
							$scope.nPage_agree += 1;
						}else{
							$scope.friends_agree = data;
						}
					}
				},function(){
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
			}//===0
			
			if($scope.selectTab==1){//==1
				if($scope.friends_disagree.page.count<=$scope.friends_disagree.member.length){
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
					return;
				}
				Myfriend.get_friend_list(2,$scope.nPage_disagree+1,function(data) {
					if(data!=null){
						if($scope.friends_disagree.member!=undefined){
							$scope.friends_disagree.member = $scope.friends_disagree.member.concat(data.member);
							$scope.nPage_disagree += 1;
						}else{
							$scope.friends_disagree = data;
						}
					}
				},function(){
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
			}//===1
			
			if($scope.selectTab==2){//==2
				if($scope.friends_apply.page.count<=$scope.friends_apply.member.length){
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
					return;
				}
				Myfriend.get_apply_list($scope.nPage_apply+1,function(data) {
					if(data!=null){
						if($scope.friends_apply.member!=undefined){
							$scope.friends_apply.member = $scope.friends_apply.member.concat(data.member);
							$scope.nPage_apply += 1;
						}else{
							$scope.friends_apply = data;
						}
					}
				},function(){
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
			}//===2
			if($scope.selectTab==3){//==3
				if($scope.friends_black.page.count<=$scope.friends_black.shielding.length){
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
					return;
				}
				Myfriend.get_black_list($scope.nPage_black+1,function(data) {
					if(data!=null){
						if($scope.friends_apply.member!=undefined){
							$scope.friends_black.member = $scope.friends_black.member.concat(data.member);
							$scope.nPage_black += 1;
						}else{
							$scope.friends_black = data;
						}
					}
				},function(){
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
			}//===3
			
		}
	/*---------------上下拉刷新------------------------*/
		
		//权限控制，切换到指定的tab页面
		$scope.init_data_auth = function() {
			Auth.get_all_user_auth(function(isHave, items) {
				if(!items['好友一览']) {//只要不是该tab页面,就切换到第2个tab页面
					$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(1);
				}
			});
		};
		
		
	}

	controller.$inject = ['$scope', 'Myfriend', '$ionicSlideBoxDelegate','GoDialog','$ionicPopup','$timeout','GoPage','Auth'];

	return app.registerController("myfriendctrl",controller);
});